<?php
/*
	广告独立佣金设置
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'广告列表'),
	array('op'=>'edit','title'=>'添加/编辑广告'),
);
set_html('tabs',$tabs);
$help='说明：填写数字表示实际佣金金额，填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放广告价格1%的佣金；';
$setting=$this->plugin_setting('setting');
$return['levels']=$this->getall('teamlevel',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
$return['levels']['0']=array('id'=>0,'title'=>'全部等级');
$return['types']=$this->adtypes;
$return['adweis']=$this->adweis;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$psize=15;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('goods',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['commissions']=$item['commissions']?json_decode($item['commissions'],true):array();
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>2、广告的真实收益，需前往腾讯广告后台查看！
		</br>3、客户广告收益：是指客户访问广告给他的收益！
		</br>4、广告收益分润：是发展直推或团队，访问广告获得收益是，团长也有收益提成！
		','list'=>array(
			array('name'=>'title','title'=>'广告名称','type'=>'string'),
			array('name'=>'placement_id','title'=>'广告ID','type'=>'string'),
			array('name'=>'image','title'=>'广告图片','type'=>'image'),
			array('name'=>'price','title'=>'广告收益','type'=>'string'),
			array('name'=>'type','title'=>'广告类型','type'=>'radio','options'=>'types'),
			array('name'=>'adwei','title'=>'广告投放地','type'=>'radio','options'=>'adweis'),
			array('name'=>'ignore','title'=>'是否关闭分红','type'=>'radio','options'=>'isyes'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>18,'action'=>'jy_ad/get_goods/','name'=>'goods_id','title'=>'选择广告'),
		),
	);
	$return['isyes']=array('否','是');
	$return['types']=$this->adtypes;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['title'] || !$post['placement_id']){
			exi('广告标题！广告ID必填','error');
		}
		$data=array(
			'title'=>$post['title'],
			'image'=>$post['image'],
			'desc'=>$post['desc'],
			'price'=>$post['price'],
			'placement_id'=>$post['placement_id'],
			'count'=>intval($post['count']),
			'adwei'=>intval($post['adwei']),
			'paixu'=>intval($post['paixu']),
			'status'=>intval($post['status']),
			'type'=>intval($post['type']),
			'ignore'=>intval($post['ignore']),
			'commissions'=>$post['commissions']?ijson_encode($post['commissions']):'',
		);
		$id=intval($post['id']);
		if($id){
			$this->update('goods',$data,array('id'=>$id));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$this->insert('goods',$data);
		}
		exi('设置成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['commissions']=$item['commissions']?json_decode($item['commissions'],true):array('commission1'=>'0.00');
	}else{
		$item=array(
			'title'=>'',
			'commissions'=>array('commission1'=>'0.00'),
		);
	}
	$commissions=array(
			array('name'=>'level_id','title'=>'适用等级','type'=>'select','help'=>'','options'=>'levels'),
			array('type'=>'unitstring','unit'=>'元','name'=>'price','title'=>'看广告收益'),
			array('type'=>'string','name'=>'nfee_per','title'=>'直推能量值','help'=>$help),
			array('type'=>'string','name'=>'team_per','title'=>'团队循环奖','help'=>$help),
			array('type'=>'string','name'=>'team_biper','title'=>'团队购物积分','help'=>$help),
	);
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'广告编辑',
		'edit'=>array(
			array('name'=>'title','title'=>'广告名称','type'=>'string'),
			array('name'=>'image','title'=>'广告图片','type'=>'image'),
			array('name'=>'desc','title'=>'广告描述','type'=>'text'),
			array('name'=>'price','title'=>'广告收益','type'=>'unitstring','unit'=>'元'),
			array('name'=>'type','title'=>'广告类型','type'=>'radio','options'=>'types'),
			array('name'=>'adwei','title'=>'广告投放地','type'=>'radio','options'=>'adweis'),
			array('name'=>'placement_id','title'=>'广告位id','type'=>'string'),
			array('name'=>'count','title'=>'每页拉取广告的数量','type'=>'unitstring','unit'=>'次','help'=>'默认三次，只适合页内插入的广告！'),
			array('name'=>'ignore','title'=>'是否关闭分红','type'=>'switch','isradio'=>1),
			array('name'=>'paixu','title'=>'广告排序','type'=>'string'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','isradio'=>1),
			
			array('type'=>'array','title'=>'广告独立分红设置','name'=>'commissions','add_tip'=>'添加一个设置',
					'list'=>$commissions,
					'details'=>$commissions
			),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$this->delete('goods',array('id'=>$id));
		}
	}
	exi('删除成功！');
}